<extend name="public/form"/>
<block name="css">
    <style>
        .layui-table-cell{
            height:auto !important;
            max-height:90px !important;
        }
    </style>
</block>
<block name="content">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
        {{$crumbs|raw}}
      </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon" style="line-height:30px">ဂ</i></a>
    </div>
    <div class="layui-card-body ">
        <button class="layui-btn layui-btn-danger layui-btn-sm delAll"><i class="layui-icon"></i>批量删除</button>
        <button class="layui-btn layui-btn-sm" onclick="parent.xadmin.add_tab('添加资讯','{{:url('add')}}')"><i class="layui-icon"></i>添加
        </button>
    </div>
    <div class="layui-card-body ">
        <table class="layui-hide" id="test" lay-filter="demo"></table>
        <script type="text/html" id="barDemo">
            <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
            <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
        </script>
    </div>
</block>
<block name="js">
    <script>
        layui.use(['table', 'jquery'], function () {
            var table = layui.table
                $ = layui.jquery,
                form = layui.form;
            table.render({
                elem: '#test'
                , url: '/admin/news/tree'
                , cellMinWidth: 70
                , cols: [[
                    {type: 'checkbox'}
                    , {field: 'id', title: 'ID', width: 90, unresize: true, sort: true}
                    , {field: 'title', width: 300,title: '资讯标题',}
                    , {field: 'news_category', title: '所属分类',templet: function (d) {
                            return d.news_category.title?d.news_category.title:"未选择分类";
                        }}
                    ,{
                        field: 'cover',title: '封面图',align: 'center',height:150
                        , templet: function (d) {
                            return "<img src=\""+d.cover+"\"></img>";
                        }
                    }
                    , {
                        title: '置顶|热门|推荐', align: 'center'
                        , templet: function (d) {
                            let top = d.top == 1 ? 'checked' : '';
                            let res='<input type="checkbox" name="top" lay-filter="top" value="' + d.id + '" lay-skin="switch" lay-text="置顶|置顶"' + top + '>';

                            let hot = d.hot == 1 ? 'checked' : '';
                            res=res+ ' <input type="checkbox" name="hot" lay-filter="hot" value="' + d.id + '" lay-skin="switch" lay-text="热门|热门"' + hot + '>';

                            let recommend = d.recommend == 1 ? 'checked' : '';
                            res=res+ ' <input type="checkbox" name="recommend" lay-filter="recommend" value="' + d.id + '" lay-skin="switch" lay-text="推荐|推荐"' + recommend + '>';
                            return res;
                        }
                    }
                    , {title: '操作', align: 'center', toolbar: '#barDemo'}
                ]]
                , page: true
            });
            //监听工具条
            table.on('tool(demo)', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case 'detail':
                        layer.msg('ID：' + data.id + ' 的查看操作');
                        break;
                    case 'del':
                        layer.confirm('确定要删除该斑斓图吗？', function (index) {
                            $.ajax({
                                url: '/admin/news/delete',
                                method: 'post',
                                data: {id:data.id},
                                dataType: 'JSON',
                                success: function (res) {
                                    if (res.code == 0) {
                                        obj.del();
                                        layer.msg('删除成功', {icon: 1});
                                    } else {
                                        layer.msg(res.msg, {icon: 5});
                                    }
                                },
                                error: function (data) {
                                    layer.msg('服务器繁忙', {icon: 5});
                                }
                            });
                            layer.close(index);
                        });
                        break;
                    case 'edit':
                        xadmin.open('编辑', '/admin/news/edit?id=' + data.id);
                        // layer.alert('编辑行：<br>'+ JSON.stringify(data))
                        break;
                    default:
                        break;
                }
            });
            //监听置顶|热门|推荐
            form.on('switch()', function (obj) {
                let data = {};
                data[this.name] = obj.elem.checked ? 1 : 0;
                let id = this.value;
                let __this=this;
                $.ajax({
                    url: '/admin/news/ajax/id/' + id,
                    method: 'post',
                    data: data,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.msg('修改成功', {icon: 1});
                        } else {
                            __this.checked=true;
                            form.render();
                            layer.msg(res.msg, {icon: 5});
                        }
                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
            });
            $('.delAll').on('click', function(){
                var checkStatus = table.checkStatus('test')
                    ,data = checkStatus.data
                    ,ids='';
                if(data.length>0){
                    for (var i = 0; i < data.length; i++) {
                        ids=ids+data[i].id+',';
                    }
                    var ids=(ids.substring(ids.length-1)==',')?ids.substring(0,ids.length-1):ids;
                    layer.confirm('确定要删除'+data.length+'个斑斓图吗？', function (index) {
                        $.ajax({
                            url: '/admin/news/delete',
                            method: 'post',
                            data: {id:ids},
                            dataType: 'JSON',
                            success: function (res) {
                                if (res.code == 0) {
                                    table.reload('test');
                                    layer.msg('删除成功', {icon: 1});
                                } else {
                                    layer.msg(res.msg, {icon: 5});
                                }
                            },
                            error: function (data) {
                                layer.msg('服务器繁忙', {icon: 5});
                            }
                        });
                        layer.close(index);
                    });
                }
            });

        });
    </script>
</block>